<?php
	echo '<script language="javascript">$prefix = "' . $user->getPrefix() . '";</script>';
	TieuChi::setEnable($user->getPrefix());
	
	$mssv = $user->getMaSo();
	if (isset($_GET['mssv']))
		$mssv = $_GET['mssv'];
?>
<form name="frmChamDRL" id="frmChamDRL" action="index.php?page=bangdiem<?php if (isset($_GET['mssv'])) echo '&mssv=' . $mssv; ?>" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php
$_rowHeader = "BẢNG ĐÁNH GIÁ KẾT QUẢ RÈN LUYỆN";
include("views/RowHeader.php");
?>
  <tr><td colspan="3">
    <table width="100%" id="bangDRL" border="1" cellpadding="5px" cellspacing="0">
      <tr bgcolor="#dae9f3">
        <th>Nội dung đánh giá</th>
        <th width="10%">SV tự cho điểm</th>
        <th width="10%">CVHT cho điểm</th>
        <th width="10%">Hội đồng Khoa</th>
      </tr>
     	<?php
		require_once("includes/database.php");
	  
	  	$db = new HocKy(true);
	  	$result = $db->select("BANG_DIEM_REN_LUYEN", array("BDRL_MA_SO"),
	  						  "BDRL_NGAY_AP_DUNG = (select max(BDRL_NGAY_AP_DUNG) from BANG_DIEM_REN_LUYEN)");
		$row = mysql_fetch_assoc($result);
		$maBangDiem = $row['BDRL_MA_SO'];
		
		$db->setQuery("SELECT B.* FROM CO_MUC A, MUC B WHERE A.BDRL_MA_SO = $maBangDiem "
					 . "AND A.MUC_MA_SO = B.MUC_MA_SO ORDER BY MUC_CO_THU_TU");
		
		$result = $db->fetchAll();
		$thuTuMuc = 1;
		
		while ($row = mysql_fetch_assoc($result)) {
			$maMuc = $MA_MUC_GOC = $row['MUC_MA_SO'];
			
			echo '<tr><td colspan="4" class="muc_goc">' . $thuTuMuc . '. ' . $row['MUC_TEN'] . '</td></tr>';

			$db->setQuery("SELECT TC_MA_SO FROM CO_TIEU_CHI WHERE MUC_MA_SO = " . $maMuc);
			$indent = 'a';
			if (mysql_num_rows($db->fetchAll()))
				$indent = '-';
			
			$db->setQuery("(SELECT TC_MA_SO as MUC_CON, TC_CO_THU_TU as THU_TU, @KHOAN := 0 as LA_KHOAN "
						 . "FROM MUC A, CO_TIEU_CHI B WHERE A.MUC_MA_SO = " . $maMuc . " and A.MUC_MA_SO = B.MUC_MA_SO) "
						 . "UNION (SELECT KHOAN_MA_SO as MUC_CON, KHOAN_CO_THU_TU as THU_TU, @KHOAN := 1 as LA_KHOAN "
						 . "FROM MUC A, CO_KHOAN C WHERE A.MUC_MA_SO = " . $maMuc . " and A.MUC_MA_SO = C.MUC_MA_SO) "
						 . "ORDER BY THU_TU");
			$supResult = $db->fetchAll();
			while ($supRow = mysql_fetch_assoc($supResult)) {
				$supMaMuc = $supRow['MUC_CON'];
				if ($supRow['LA_KHOAN'] == 1) {
					$db->setQuery("SELECT MUC_TEN FROM MUC WHERE MUC_MA_SO = " . $supMaMuc);
					$thongTinKhoan = mysql_fetch_assoc($db->fetchAll());
					
					if ($indent == '-')
						echo '<tr><td>- ' . $thongTinKhoan['MUC_TEN'].'</td><td></td><td></td><td></td></tr>';
					else echo '<tr><td><b>' . $indent++ . '. ' . $thongTinKhoan['MUC_TEN'].'</b></td><td></td><td></td><td></td></tr>';
					
					$db->setQuery("(SELECT TC_MA_SO as MUC_CON, TC_CO_THU_TU as THU_TU, @KHOAN := 0 as LA_KHOAN "
						 . "FROM MUC A, CO_TIEU_CHI B WHERE A.MUC_MA_SO = " . $supMaMuc . " and A.MUC_MA_SO = B.MUC_MA_SO) "
						 . "UNION (SELECT KHOAN_MA_SO as MUC_CON, KHOAN_CO_THU_TU as THU_TU, @KHOAN := 1 as LA_KHOAN "
						 . "FROM MUC A, CO_KHOAN C WHERE A.MUC_MA_SO = " . $supMaMuc . " and A.MUC_MA_SO = C.MUC_MA_SO) "
						 . "ORDER BY THU_TU");
					$infResult = $db->fetchAll();
					
					while ($infRow = mysql_fetch_assoc($infResult)) {
						$infMaMuc = $infRow['MUC_CON'];
						//echo '<tr><td>'.$infMaMuc.'</td></tr>';
						
						if ($infRow['LA_KHOAN'] == 1) {
							$db->setQuery("SELECT MUC_TEN FROM MUC WHERE MUC_MA_SO = " . $infMaMuc);
							$khoanCon = mysql_fetch_assoc($db->fetchAll());
							echo '<tr><td>- ' . $khoanCon['MUC_TEN'].'</td><td></td><td></td><td></td></tr>';
							
							$db->setQuery("SELECT TC_MA_SO FROM CO_TIEU_CHI WHERE MUC_MA_SO = " . $infMaMuc
										  . " ORDER BY TC_CO_THU_TU");
							$TCResult = $db->fetchAll();
							while ($TCRow = mysql_fetch_assoc($TCResult))
								TieuChi::formatRow(2, $TCRow['TC_MA_SO'], $mssv, $db->getMaHocKy(), $MA_MUC_GOC, $thuTuMuc, $db);
						} else {
							if ($indent == '-')
								TieuChi::formatRow(2, $infMaMuc, $mssv, $db->getMaHocKy(), $MA_MUC_GOC, $thuTuMuc, $db);
							else TieuChi::formatRow(1, $infMaMuc, $mssv, $db->getMaHocKy(), $MA_MUC_GOC, $thuTuMuc, $db);
						}
					}
					
				} else TieuChi::formatRow(1, $supMaMuc, $mssv, $db->getMaHocKy(), $MA_MUC_GOC, $thuTuMuc, $db);
			}
			
			echo '<tr class="tong_diem_muc">';
			echo '<td id="MUC_' . $MA_MUC_GOC . '_DIEM_' . $row['MUC_DIEM_TOI_DA'] . '">';
			echo 'Điểm cộng tối đa của mục ' . $thuTuMuc++ . ' là ' . $row['MUC_DIEM_TOI_DA'] . ' điểm';
			echo '</td>';
			echo '<td id="SV_TongDiemMuc_' . $MA_MUC_GOC . '">0</td>';
			echo '<td id="CVHT_TongDiemMuc_' . $MA_MUC_GOC . '">0</td>';
			echo '<td id="HDK_TongDiemMuc_' . $MA_MUC_GOC . '">0</td></tr>';
			
			echo '<tr bgcolor="white"><td colspan="4"></td></tr>';
		} // while cua MUC
		?>
        <tr class="muc_goc" style="color:#F00" align="center">
          <td align="right"><strong>TỔNG ĐIỂM</strong></td>
          <td id="SV_TONG_DIEM">0</td>
          <td id="CVHT_TONG_DIEM">0</td>
          <td id="HDK_TONG_DIEM">0</td>
        </tr>
    </table>
  </td></tr>
  <tr><td colspan="3">&nbsp;</td></tr>
  <tr align="center"><td colspan="3">
  <?php
  if ($user->getPrefix() != "SV_")
  	echo '<input type="button" value="Sao chép từ điểm của sinh viên" class="button_1" onclick="saoChepTuDiemSinhVien();" />&nbsp;';
  ?>
  <input type="submit" value="Chấm điểm" class="button_1" /></td></tr>
  <tr><td colspan="3">&nbsp;</td></tr>
</table>
</form>

<?php
//var_dump(TieuChi::layDanhSachMaTieuChi($db));
?>